package chapter07

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/9 9:07 PM
 */
object Test01_ImmutableArray {
  def main(args: Array[String]): Unit = {
    val arr = new Array[Int](5)
    val arr2 = Array[Int](10, 11)

    // 2.访问元素
    arr(0) = 10
    arr(4) = 2

    // 3.数组遍历
    // 1) for
    for (i <- 0 to arr.length - 1) {
      print(arr(i))
    }

    for (i <- arr.indices) {
      print(arr(i))
    }
    println("=" * 20)
    // 2) 直接遍历所有元素
    for (elem <- arr) {
      print(elem)
    }
    println("=" * 20)
    // 3) 迭代器
    val iter = arr2.iterator
    while (iter.hasNext) {
      print(iter.next())
    }
    println("=" * 20)
    // 4) foreach
    arr.foreach(print)
    println("=" * 20)
    println(arr.mkString(","))

    // 4.添加元素
    val newArr = arr2.:+(14)
    println(newArr.mkString("-"))
    val newArr2 = newArr.+:(15)
    println(newArr2.mkString("-"))

    val newArr3 = newArr2 :+ 16
    val newArr4 = 19 +: 29 +: newArr3 :+ 18 :+ 17
    println(newArr4.mkString("-"))
  }
}
